package cn.itsource.pethome.org.controller;

import cn.itsource.pethome.org.domain.Employee;
import cn.itsource.pethome.org.query.EmployeeQuery;
import cn.itsource.pethome.org.service.IEmployeeService;
import cn.itsource.pethome.util.AjaxResult;
import cn.itsource.pethome.util.BusinessException;
import cn.itsource.pethome.util.PageList;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/emp")//设置了一个根路径
@Api(value = "员工的API",description="员工相关的CRUD功能")
public class EmployeeController {
    @Autowired
    private IEmployeeService employeeService;

    @PostMapping("/pagelist")
    public PageList<Employee> pageQueryAll(@RequestBody EmployeeQuery query) {
        return employeeService.pageQueryAll(query);
    }

    @PutMapping
    public AjaxResult saveEmp(@RequestBody Employee employee){
        try {
            if(employee.getId() == null){
                employeeService.insert(employee);
            }else {
                employeeService.update(employee);
            }
            return AjaxResult.me();
        }catch (BusinessException e){//你的业务出现问题了
            return AjaxResult.error().setMsg(e.getMessage());
        }catch (Exception e) {//系统级别的错误
            e.printStackTrace();
            return AjaxResult.error();
        }
    }

    @DeleteMapping("/{id}")
    public AjaxResult deleteEmp(@PathVariable("id")Long id){
        try {
            employeeService.delete(id);
            return AjaxResult.me();
        }catch (BusinessException e){//你的业务出现问题了
            return AjaxResult.error().setMsg(e.getMessage());
        }catch (Exception e) {//系统级别的错误
            e.printStackTrace();
            return AjaxResult.error();
        }
    }

    @GetMapping("/{shopid}")
    public List<Employee> queryEmpByShopId(@PathVariable("shopid")Long shopid){
       return employeeService.queryEmpByShopId(shopid);
    }
}
